{{>licenseInfo}}
package {{apiPackage}}

{{#imports}}import {{import}}
{{/imports}}

import {{packageName}}.infrastructure.*
import io.ktor.client.request.forms.formData
import io.ktor.client.engine.HttpClientEngine
import io.ktor.client.features.json.serializer.KotlinxSerializer
import kotlinx.serialization.json.Json
import io.ktor.http.ParametersBuilder
import kotlinx.serialization.*
import kotlinx.serialization.descriptors.*
import kotlinx.serialization.encoding.*

{{#operations}}
{{#nonPublicApi}}internal {{/nonPublicApi}}class {{classname}}(
    baseUrl: String = ApiClient.BASE_URL,
    httpClientEngine: HttpClientEngine? = null,
    jsonSerializer: Json = ApiClient.JSON_DEFAULT
) : ApiClient(baseUrl, httpClientEngine, jsonSerializer) {

    {{#operation}}
    /**
     * {{summary}}
     * {{notes}}
    {{#allParams}} * @param {{{paramName}}} {{description}} {{^required}}(optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}}
    {{/allParams}} * @return {{{returnType}}}{{^returnType}}void{{/returnType}}
     */
    {{#returnType}}
    @Suppress("UNCHECKED_CAST")
    {{/returnType}}
    suspend fun {{operationId}}({{#allParams}}{{{paramName}}}: {{{dataType}}}{{^required}}?{{/required}}{{^-last}}, {{/-last}}{{/allParams}}): HttpResponse<{{{returnType}}}{{^returnType}}Unit{{/returnType}}> {

        val localVariableAuthNames = listOf<String>({{#authMethods}}"{{name}}"{{^-last}}, {{/-last}}{{/authMethods}})

        val localVariableBody = {{#hasBodyParam}}{{#bodyParam}}{{#isArray}}{{operationIdCamelCase}}Request({{{paramName}}}{{^isList}}.asList(){{/isList}}){{/isArray}}{{^isArray}}{{#isMap}}{{operationIdCamelCase}}Request({{{paramName}}}){{/isMap}}{{^isMap}}{{{paramName}}}{{/isMap}}{{/isArray}}{{/bodyParam}}{{/hasBodyParam}}
            {{^hasBodyParam}}
            {{#hasFormParams}}
            {{#isMultipart}}
            formData {
                {{#formParams}}
                {{{paramName}}}?.apply { append("{{{baseName}}}", {{{paramName}}}) }
                {{/formParams}}
            }
            {{/isMultipart}}
            {{^isMultipart}}
            ParametersBuilder().also {
                {{#formParams}}
                {{{paramName}}}?.apply { it.append("{{{baseName}}}", {{{paramName}}}.toString()) }
                {{/formParams}}
            }.build()
            {{/isMultipart}}
            {{/hasFormParams}}
            {{^hasFormParams}}
            io.ktor.client.utils.EmptyContent
            {{/hasFormParams}}
            {{/hasBodyParam}}

        val localVariableQuery = mutableMapOf<String, List<String>>()
        {{#queryParams}}
        {{{paramName}}}?.apply { localVariableQuery["{{baseName}}"] = {{#isContainer}}toMultiValue(this, "{{collectionFormat}}"){{/isContainer}}{{^isContainer}}listOf("${{{paramName}}}"){{/isContainer}} }
        {{/queryParams}}

        val localVariableHeaders = mutableMapOf<String, String>()
        {{#headerParams}}
        {{{paramName}}}?.apply { localVariableHeaders["{{baseName}}"] = {{#isContainer}}this.joinToString(separator = collectionDelimiter("{{collectionFormat}}")){{/isContainer}}{{^isContainer}}this.toString(){{/isContainer}} }
        {{/headerParams}}

        val localVariableConfig = RequestConfig<kotlin.Any?>(
            RequestMethod.{{httpMethod}},
            "{{path}}"{{#pathParams}}.replace("{" + "{{baseName}}" + "}", "${{{paramName}}}"){{/pathParams}},
            query = localVariableQuery,
            headers = localVariableHeaders
        )

        return {{#hasBodyParam}}jsonRequest{{/hasBodyParam}}{{^hasBodyParam}}{{#hasFormParams}}{{#isMultipart}}multipartFormRequest{{/isMultipart}}{{^isMultipart}}urlEncodedFormRequest{{/isMultipart}}{{/hasFormParams}}{{^hasFormParams}}request{{/hasFormParams}}{{/hasBodyParam}}(
            localVariableConfig,
            localVariableBody,
            localVariableAuthNames
        ).{{#isArray}}wrap<{{operationIdCamelCase}}Response>().map { value{{^isList}}.toTypedArray(){{/isList}} }{{/isArray}}{{^isArray}}{{#isMap}}wrap<{{operationIdCamelCase}}Response>().map { value }{{/isMap}}{{^isMap}}wrap(){{/isMap}}{{/isArray}}
    }

{{#hasBodyParam}}
{{#bodyParam}}
{{#isArray}}{{>serial_wrapper_request_list}}{{/isArray}}{{#isMap}}{{>serial_wrapper_request_map}}{{/isMap}}
{{/bodyParam}}
{{/hasBodyParam}}
{{#isArray}}
{{>serial_wrapper_response_list}}
{{/isArray}}
{{#isMap}}
{{>serial_wrapper_response_map}}
{{/isMap}}

    {{/operation}}
}
{{/operations}}
